package com.fire.controller;


import com.fire.exception.ServiceException;
import com.fire.model.entity.Notice;
import com.fire.response.ResultData;
import com.fire.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-05
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequiredArgsConstructor
@Api(tags = {"数据管理模块"})
@RequestMapping("/notice")
public class NoticeController {

    private final INoticeService noticeService;

    /**
     * 查询所有通知
     * @return
     */
    @GetMapping("/query/all")
    @ApiOperation(value = "查询所有通知")
    public ResultData queryAll() {
        log.info("查询所有通知请求.....");
        List<Notice> list = noticeService.queryAll();
        log.info("查询所有通知请求成功...");
        return ResultData.success(list);
    }

    /**
     * 未读消息查询
     * @return
     */
    @GetMapping("/query/unread")
    @ApiOperation(value = "未读消息查询")
    public ResultData queryUnReadNotice() {
        log.info("未读消息查询请求.....");
        Map<String,Object> map = noticeService.queryUnReadNotice();
        log.info("未读消息查询请求成功...");
        return ResultData.success(map);
    }

    /**
     * 已读消息查询
     * @return
     */
    @GetMapping("/query/read")
    @ApiOperation(value = "已读消息查询")
    public ResultData queryReadNotice() {
        log.info("已读消息查询请求.....");
        List<Notice> list = noticeService.queryReadNotice();
        log.info("已读消息查询请求成功...");
        return ResultData.success(list);
    }

    /**
     * 修改一条消息为已读状态
     * @return
     */
    @PutMapping("/update/one/{id}")
    @ApiOperation(value = "修改未读消息(一条)")
    public ResultData updateOne(@PathVariable("id") Integer id) throws ServiceException {
        noticeService.updateOne(id);
        return ResultData.success();
    }

    /**
     * 修改一条消息为已读 / 未读状态
     * @return
     */
    @PutMapping("/update/{id}")
    @ApiOperation(value = "修改已读 / 未读消息(一条)")
    public ResultData update(@PathVariable("id") Integer id) throws ServiceException {
        noticeService.updateReadOrRuread(id);
        return ResultData.success();
    }

    /**
     * 修改全部未读消息为已读状态
     * @return
     */
    @PutMapping("/update/all")
    @ApiOperation(value = "修改未读消息")
    public ResultData updateAll(){
        noticeService.updateAll();
        return ResultData.success();
    }

    /**
     * 删除消息
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除消息")
    public ResultData delete(@PathVariable("id") Integer id){
        noticeService.removeById(id);
        return ResultData.success();
    }
}
